package com.util.gzp.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ClassName: FindDate
 * Package: com.bestescortbedsystem.util
 *
 * @author: guanzepeng
 * @Date: 2021/2/22 16:33 星期一
 * @Description: 获取当前时间并格式化输出
 */
public class FindDate {

	/**
	 * 此类不需要实例化
	 */
	private FindDate() {}

	private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private static final ThreadLocal<DateFormat> THREAD_LOCAL = new ThreadLocal<DateFormat>();

	public static DateFormat getDateFormat()
	{
		DateFormat df = THREAD_LOCAL.get();
		if(df==null){
			df = new SimpleDateFormat(DATE_FORMAT);
			THREAD_LOCAL.set(df);
		}
		return df;
	}

	/**
	 * 获取到当前的时间,将其格式化,然后转换为String格式
	 * @return 返回当前时间
	 */
	public static String getDate(){
		Date date = new Date(System.currentTimeMillis());
		String format = getDateFormat().format(date);
		THREAD_LOCAL.remove();
		return format;
	}

	/**
	 * 根据传入的时间格式化为String
	 * @param date
	 * @return
	 */
	public static  String dateTimeString(Date date) {
		String date2 = getDateFormat().format(date);
		THREAD_LOCAL.remove();
		return date2;
		}

	/**
	 * 根据传入的时间格式化为Date
	 * @param date
	 * @return
	 * @throws ParseException
	 */
		public static Date getDate(String date) throws ParseException {
			Date parse = getDateFormat().parse(date);
			THREAD_LOCAL.remove();
			return parse;
		}

	}
